package recordProcessing;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

import table.TableCollection;

import fileProcessing.ParsingException;

public class RecordCollection {
	private final static RecordCollection recordCollection = new RecordCollection();
	private HashMap<String,Record> collection = new HashMap<String,Record>(1000000);
	
	private RecordCollection(){}
	
	public RecordCollection getCollection(){
		return recordCollection;
	}
	
	public void AddRecord(Record record){
		collection.put(record.getRecordID(), record);
	}
	
	/**
	 * Provides one instance of TableCollection for use throughout the project
	 * @return Instance of TableCollection
	 */
	public static RecordCollection getRecordHashClass(){
		return recordCollection;
	}
	
	public void outputToCSV(String delimiter) throws ParsingException{
		BufferedWriter file_CSV;
		try {
			file_CSV = new BufferedWriter(new FileWriter("CurrentOutput.csv"));
			for(String r : collection.keySet()){
				file_CSV.write(collection.get(r).toCSV(delimiter));
			}
			
		} catch (IOException e) {
			throw new ParsingException("Error writing CSV file for record: "+ e.getMessage());
		}
	}
}
